package com.boc.langxiang.presenter.adm;


import com.boc.langxiang.contract.adm.AdmSimpleContract;
import com.boc.langxiang.entity.events.AdmFollowEvent;
import com.boc.langxiang.entity.request.AdmDetailRequest;
import com.boc.langxiang.entity.response.AdmDetailContent;
import com.boc.langxiang.entity.response.AdmDetailEntity;
import com.boc.langxiang.entity.response.AdmFollowContent;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import org.greenrobot.eventbus.EventBus;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class AdmSimplePresenter implements AdmSimpleContract.Presenter {

    private final ApiService mService;
    private AdmSimpleContract.View mView;

    public AdmSimplePresenter(AdmSimpleContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }


    @Override
    public void admDetail(String designerId) {
        mView.showLoading();
        AdmDetailRequest request = new AdmDetailRequest();
        request.designerId = designerId;
        Observable<BaseResponse<AdmDetailContent>> register = mService.admDetail(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<AdmDetailContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<AdmDetailContent> response) {
                        AdmDetailContent content = response.getContent();
                        if (content != null) {
                            AdmDetailEntity res = content.getRes();
                            mView.admDetailSuccess(res);
                        }
                    }
                });
    }

    @Override
    public void admFollow(String designerId) {
//        mView.showLoading();
        AdmDetailRequest request = new AdmDetailRequest();
        request.designerId = designerId;
        Observable<BaseResponse<AdmFollowContent>> register = mService.admFollow(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<AdmFollowContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<AdmFollowContent> response) {
                        AdmFollowContent content = response.getContent();
                        if (content != null) {
                            AdmFollowEvent event = new AdmFollowEvent(content.getRes());
                            EventBus.getDefault().post(event);
                        }
                    }
                });
    }
}
